<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="showPermission(1, '0')">
              添加权限
            </el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="list" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-show="row.type === 1"
                type="text"
                @click="showPermission(2, row.id)"
                >添加</el-button
              >
              <el-button type="text" @click="editPermission(row)">
                编辑
              </el-button>
              <el-button type="text" @click="delPermission(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加权限弹层 -->
    <el-dialog
      :visible="showDialog"
      :title="formData.id ? '编辑权限' : '新增权限'"
      @close="closeDialog"
    >
      <el-form
        ref="permissionForm"
        label-width="100px"
        :model="formData"
        :rules="rules"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="addPermission">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission.js'
import { tranListToTreeDate } from '@/utils/index.js'
export default {
  name: 'PermissionIndex',
  data () {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }

    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    /**
     * 编辑权限
     * 1.和添加共有dialog弹层
     * 2.根据id重新调取数据详情进行回显
     * 3.在提交表单里调更新接口
     */
    async editPermission (row) {
      const detail = await getPermissionDetail(row.id)
      // 数据回显: 数据驱动视图
      this.formData = detail
      this.showDialog = true
    },
    /**
     * 删除当前权限
     * curr:{} 当前权限的信息
     * 1.删除敏感操作===>确认框
     * 2.如果删除的权限里面还有子权限则不能删除  ===>判断有没children数组 并且长度大于0
     * 3.重刷列表
     * 4.信息提示
     */
    async delPermission (curr) {
      try {
        await this.$confirm(`确认删除===>${curr.name}权限 吗?`, '温馨提示')
        // 点击了确认判断是否有子权限
        if (curr.children && curr.children.length) {
          return this.$message.warning('不能直接删除有功能权限的页面权限哦!')
        }
        // 删除成功 用id传参调用接口
        await delPermission(curr.id)
        this.getPermissionList()
        this.$message.success(`删除${curr.name}  成功q(≧▽≦q)`)
      } catch (error) {
        console.log('删除权限', error)
      }
    },
    /**
     * 添加权限
     * 1.表单整体效验
     * 2.新增接口
     * 3.刷新列表数据
     * 4.关闭弹窗,成功提示,清空列表效验
     */
    async addPermission () {
      try {
        await this.$refs.permissionForm.validate()
        // 根据formData唯一id判断此时是在添加还是在编辑
        if (this.formData.id) {
          // 编辑更新
          await updatePermission(this.formData)
        } else {
          // 添加权限
          await addPermission(this.formData)
        }

        this.getPermissionList()
        this.$message.success(this.formData.id ? '编辑成功' : `新增${this.formData.name}成功`)
        this.showDialog = false
      } catch (error) {
        this.$message.error('请完成效验规则')
      }
    },
    /**
     * 打开权限弹层并且确认是什么权限
     * 1.打开权限弹窗
     * 2.根据type判断是页面权限还是功能权限
     */
    showPermission (type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    async getPermissionList () {
      const data = await getPermissionList()
      this.list = tranListToTreeDate(data)
      // console.table(this.list)
    },
    /**
     * dialog弹层关闭事件
     * 关闭弹层
     * 清空 表单 和 表单效验
     */
    closeDialog () {
      this.showDialog = false
      this.$refs.permissionForm.resetFields()
      this.formData = {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
